﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_between_enumerables_inheriting_from_GenericICollection
    {
        private readonly Mapping<StandardPerson, CustomPerson> mapping;

        public class StandardEntityObject : EntityObject
        {
            public String Name { get; set; }
        }

        public class StandardPerson
        {
            public List<StandardEntityObject> ListWithEntityObjects { get; set; }
        }

        public class CustomEntityObject : EntityObject
        {
            public String Name { get; set; }
        }

        public class CustomPerson
        {
            public EntityCollection<CustomEntityObject> EntityCollection { get; set; }
        }

        public When_mapping_between_enumerables_inheriting_from_GenericICollection()
        {
            var entityObjectMapping = new Mapping<StandardEntityObject, CustomEntityObject>(x => new StandardEntityObject(), y => new CustomEntityObject());
            entityObjectMapping.Relate(x => x.Name, y => y.Name);

            mapping = new Mapping<StandardPerson, CustomPerson>();
            mapping.Relate(from => from.ListWithEntityObjects, to => to.EntityCollection, entityObjectMapping);
        }

        [Fact]
        public void Should_map_from_list_inheriting_from_GenericICollection()
        {
            var fromPerson = new CustomPerson { EntityCollection = new EntityCollection<CustomEntityObject> { new CustomEntityObject { Name = "1" } } };
            var toPerson = mapping.Map(fromPerson, new StandardPerson());

            Assert.Equal(1, toPerson.ListWithEntityObjects.Count);
            Assert.Equal("1", toPerson.ListWithEntityObjects[0].Name);
        }

        [Fact]
        public void Should_map_to_list_inheriting_from_GenericICollection()
        {
            var fromPerson = new StandardPerson { ListWithEntityObjects = new List<StandardEntityObject> { new StandardEntityObject { Name = "1" } } };
            var toPerson = mapping.Map(fromPerson, new CustomPerson());

            Assert.Equal(1, toPerson.EntityCollection.Count);
            var enu = toPerson.EntityCollection.GetEnumerator();
            enu.MoveNext();
            var firstOccurence = enu.Current;
            Assert.Equal("1", firstOccurence.Name);
        }
    }
}
